/**
 *  \file   main.c
 *  \brief  eZ430-RF2500 tutorial
 *  \author Miora ANDRIAMANOHARISOA, Loic MINGHETTI
 *  \date   PFE 2011
 **/

#include "include.h"


int main(void) {


	leds_init();
	led_red_on();
	
	// init watchdog
    WDTCTL = WDT_ARST_1000;  // reset after 1000 ms

	// init timers and clocks
	timers_init();
	
	// init flash
	flash_allocation_init();
	
	// init drivers
	ui_init();  // button and uart
	
	// init temperature sensor
	sensor_init();
	
	// init radio
	xmac_init();


    __enable_interrupt();
    
    
	
	
	// init protothreads
	#define NUM_PT 4
	static struct pt pt[NUM_PT];
	int i;
	for(i = 0; i < NUM_PT; i++) {
		PT_INIT(&pt[i]);
	}


	led_green_on();

    /* simple cycle scheduling */
    while(1) {
        thread_xmac_rx(&pt[0]);
        thread_xmac_tx(&pt[1]);
        thread_sensor(&pt[2]);
        thread_ui(&pt[3]);
        
        // detect cycle overshoot
        TIMER_LPM--;
        if ((signed int)TIMER_LPM <= 0) {
        	TIMER_LPM = 0;
        	// sleep only if cycle has not been overshot
        	LPM(0);
        }
        
    	// reset watchdog
    	WDTCTL = WDT_ARST_1000;
        
    }
}
